//
//  MonthViewController.m
//  One
//
//  Created by mac on 16/5/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MonthViewController.h"
#import "ViewByMonthViewController.h"

@interface MonthViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    UITableView *monthTable;
    NSMutableArray *mounthArray;
    UIColor *tintColor;
}

@end

@implementation MonthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    tintColor = [UIColor colorWithRed:116/255.0 green:116/255.0 blue:116/255.0 alpha:1];
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    [self.navigationController.navigationBar setTintColor:tintColor];
        
    int month = _currentMonth;
    int year = _currentYear;
    
    self.title = @"往期列表";
    self.view.backgroundColor = [UIColor redColor];
    
    monthTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    monthTable.delegate = self;
    monthTable.dataSource = self;
    [self.view addSubview:monthTable];
    
    mounthArray = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < _monthCount; i ++) {
        if (i == 0) {
            [mounthArray addObject:@"本月"];
        }
        else
        {
            month --;
            if (month == 0) {
                month = 12;
                year --;
            }
            NSString *date = nil;
            if (month<10) {
                date = [NSString stringWithFormat:@"%d-0%d",year,month];
            }
            else
                date = [NSString stringWithFormat:@"%d-%d",year,month];
            [mounthArray addObject:date];
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _monthCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
    
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = mounthArray[indexPath.row];
    cell.textLabel.textColor = tintColor;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    ViewByMonthViewController *viewByMonth = [[ViewByMonthViewController alloc]init];
    NSString *str = nil;
    if(indexPath.row != 0)
    {
        
        str = cell.textLabel.text;
    }
    else
    {
        if (_currentMonth<10) {
            str = [NSString stringWithFormat:@"%d-0%d",_currentYear,_currentMonth];
        }
        else
            str = [NSString stringWithFormat:@"%d-%d",_currentYear,_currentMonth];
    }
    viewByMonth.monthStr = str;
    [viewByMonth setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:viewByMonth animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
